/**
 * Created with IntelliJ IDEA.
 * Description:验证线程安全
 * User: 东莞呵呵
 * Date:2022-09-02
 * Time:11:17
 */
public class Demo5 {
    public static synchronized void increase(){
        sum++;
    }
    public static int sum=0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 =new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    increase();
                }
            }
        });
        Thread t2 =new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    increase();;
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(sum);
    }
}
